package com.huawei.crm.web.controller;

import com.huawei.crm.domain.Customertracehistory;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.query.CustomerTraceHistoryQueryObject;
import com.huawei.crm.service.ICustomertracehistoryService;
import com.huawei.crm.service.ISystemdictionaryitemService;
import com.huawei.crm.util.JsonResult;
import com.huawei.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTraceHistory")
public class CustomertracehistoryController {

    @Autowired
    private ICustomertracehistoryService service;

    @Autowired
    private ISystemdictionaryitemService systemdictionaryitemService;

    @RequestMapping("/list")
    // value: 指定当前方法需要的权限
    // logical: 设置当前用户是否是必须拥有所有的权限才能访问当前的方法
    // AND: 同时拥有,默认值
    // OR: 拥有其中任何一个即可
    @RequiresPermissions(value = {"客户跟进历史列表","customerTraceHistory:list"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") CustomerTraceHistoryQueryObject qo) {
        // 获取当前用户
        Employee currentEmp = UserContext.getCurrentEmp();
        // 判断当前用户是否是超管或经理
        if(!(currentEmp.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))){
            qo.setSellerId(currentEmp.getId());
        }
        model.addAttribute("pageInfo", service.query(qo));

        // 查询跟进方式
        model.addAttribute("types",systemdictionaryitemService.listItemsByDictionarySn("communicationMethod"));
        return "customerTracehistory/list";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"客户跟进历史保存或修改","customerTraceHistory:saveOrUpdate"}, logical = Logical.OR)
    @ResponseBody // 将返回值转换成json格式的字符串并响应给页面
    public JsonResult saveOrUpdate(Customertracehistory d) {
        JsonResult result = new JsonResult();
        try {
            service.saveOrUpdateC(d);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,操作失败!");
        }
        return result;
    }
}
